//
//  TF_LivingNavigationBar.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/4/7.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_LivingNavigationBar: TalkfunHitTestView {
    @objc public let view = TalkfunHitTestView()
    //浮动广告列表
    @objc public let adListView = TF_AdListView()
    //直播关闭
    @objc public let livingClose = UIButton()
    //直播刷新
    @objc public let livingRefresh = UIButton()
    //邀请榜单UIButton
    @objc public let invitationList = TF_InvitationList()
    @objc public var mNavigationbarHeight:CGFloat = 0
    @objc public let zhuboView = UIView()
    //主播头像
    @objc public let avatarImage = UIImageView()
    //主播名字
    @objc public let nicknameLabel = UILabel()
    //在线观看人数
    @objc public let viewersLabel = UILabel()
    //关注
    @objc public let attentionButton = UIButton()
    //关注的背景
    @objc public let attentionButtonbackgroundColor = UIView()
    //时钟
    @objc public let clocKButton = UIButton()
    //距离直播还有
    @objc public let clocKLabel = UILabel()
    //日期内容
    @objc public let dateContentLabel = UILabel()
    //主播暂时不在
    @objc public let leaveTemporarilyLabel = UILabel()
    //直播结束
    @objc public let liveBroadcastClosedImage = UIImageView()
    //直播结束label
    @objc public let liveBroadcastClosedLabel = UILabel()
    //滚动通知
    @objc public let announceRollView = TF_AnnounceRollView()
    
    //观看
    let watchLabel = UILabel()
    let gl = CAGradientLayer()
    var startTime:NSInteger = 0
    var timer:Timer? = nil
 
    override init(frame: CGRect) {
        super.init(frame: frame)
        //TODO:浮动广告列表VIew
    
        self.adListView.backgroundColor = UIColor.clear
        self.addSubview(self.adListView)

        self.addSubview(self.view)
        //TODO:退出直播间
        self.livingClose.setImage(UIImage.init(named: "HTLive.bundle/LivingClose"), for: .normal)
        self.livingClose.backgroundColor = UIColor.clear
        self.view.addSubview(self.livingClose)
    
        //TODO:
        self.zhuboView.backgroundColor = UIColor(red: 0/255.0 , green: 0/255.0 , blue: 0/255.0 , alpha: 0.3)
        self.view.addSubview(self.zhuboView)
        
        //TODO:邀请榜单
        self.invitationList.isHidden = true
        self.invitationList.backgroundColor = UIColor(red: 0/255.0 , green: 0/255.0 , blue: 0/255.0 , alpha: 0.3)
        self.invitationList.layer.cornerRadius = 11.5
        self.view.addSubview(self.invitationList)
        
        //TODO:刷新
        self.livingRefresh.setImage(UIImage.init(named: "HTLive.bundle/livingRefresh"), for: .normal)
        self.livingRefresh.backgroundColor = UIColor.clear
        self.addSubview(self.livingRefresh)
        
        //TODO:主播头像
        self.avatarImage.contentMode =  .scaleAspectFill;
        self.avatarImage.backgroundColor = UIColor.clear
        self.zhuboView.addSubview(self.avatarImage)
        //TODO:主播名字
        self.nicknameLabel.numberOfLines = 0
        self.zhuboView.addSubview(self.nicknameLabel)
        self.nicknameLabel.font = UIFont.PingFang(size: 14)

        self.nicknameLabel.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
     
        ////在线观看人数
        self.viewersLabel.numberOfLines = 0;
        self.zhuboView.addSubview(self.viewersLabel)
        self.viewersLabel.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        self.viewersLabel.font = UIFont.PingFang(size: 10)
    
        self.zhuboView.addSubview(self.attentionButtonbackgroundColor)
        self.attentionButtonbackgroundColor.layer.addSublayer(self.gl)
      
        //关注
        self.attentionButtonbackgroundColor.isHidden = true
        self.attentionButton.isHidden = true
        self.zhuboView.addSubview(self.attentionButton)
        self.attentionButton.setTitle("关注", for: .normal)
        self.attentionButton.titleLabel!.font = UIFont.PingFang(size: 14)
        
        //时钟
        self.clocKButton.isHidden = true
        self.addSubview(self.clocKButton)
        self.clocKButton.setImage(UIImage.init(named: "HTLive.bundle/TalkfunLivingClock"), for: .normal)
      
        //距离直播还有
        self.clocKLabel.isHidden = true
        self.clocKLabel.textAlignment =  .center;
        self.clocKLabel.numberOfLines = 0;
        self.addSubview(self.clocKLabel)

        self.clocKLabel.attributedText = NSMutableAttributedString.getAttributed(string: "距离直播还有", font: UIFont.PingFang(size: 16), foregroundColor: UIColor(red: 177/255.0, green: 209/255.0, blue: 248/255.0, alpha: 1));
        self.clocKLabel.textColor = UIColor(red: 177/255.0, green: 209/255.0, blue: 248/255.0, alpha: 1)
     
        //日期内容
        self.dateContentLabel.numberOfLines = 0;
        self.addSubview(self.dateContentLabel)
        self.dateContentLabel.font = UIFont.PingFang(size: 18)
        self.dateContentLabel.textColor = UIColor(red: 177/255.0, green: 209/255.0, blue: 248/255.0, alpha: 1)
        self.dateContentLabel.textAlignment =  .center;

        //暂时不在
        self.leaveTemporarilyLabel.isHidden = true
        self.leaveTemporarilyLabel.numberOfLines = 0;
        self.addSubview(self.leaveTemporarilyLabel)

        self.leaveTemporarilyLabel.textAlignment = .center;
        self.leaveTemporarilyLabel.attributedText =  NSMutableAttributedString.getAttributed(string: "主播暂时不在，稍后再来", font: UIFont.PingFang(size: 18), foregroundColor: UIColor(red: 177/255.0, green: 209/255.0, blue: 248/255.0, alpha: 1))
        self.leaveTemporarilyLabel.textColor = UIColor(red: 177/255.0, green: 209/255.0, blue: 248/255.0, alpha: 1)

        //TODO:直播结束图片

        self.liveBroadcastClosedImage.isHidden = true
        self.liveBroadcastClosedImage.image = UIImage.init(named: "HTLive.bundle/LiveBroadcastClosed")
      
        self.liveBroadcastClosedImage.contentMode =  .scaleToFill;
//        self.liveBroadcastClosedImage.backgroundColor = UIColor.clear
//        self.addSubview(self.liveBroadcastClosedImage)
        
        //TODO:直播结束UILabel
        self.liveBroadcastClosedLabel.isHidden = true
        self.liveBroadcastClosedLabel.numberOfLines = 0;
        self.addSubview(self.liveBroadcastClosedLabel)
        self.liveBroadcastClosedLabel.textAlignment = .center

        self.liveBroadcastClosedLabel.attributedText = NSMutableAttributedString.getAttributed(string: "直播已结束", font: UIFont.PingFang(size: 18), foregroundColor: UIColor(red: 177/255.0, green: 209/255.0, blue: 248/255.0, alpha: 1));
        self.liveBroadcastClosedLabel.textColor = UIColor(red: 177/255.0, green: 209/255.0, blue: 248/255.0, alpha: 1)
        self.liveBroadcastClosedLabel.alpha = 1.0
    
        //TODO:滚动通知
        self.addSubview(self.announceRollView)
        
    }
    
    @objc public func timerStop()
    {
        if((self.timer) != nil)
        {
            self.timer!.invalidate()
            self.timer = nil
        }
        //时钟
        self.clocKButton.isHidden = true
        //距离直播还有
        self.clocKLabel.isHidden = true
        //日期内容
        self.dateContentLabel.isHidden = true
        
        self.announceRollView.timerStop()
    }
    func timeIntervalString(until timestamp: NSInteger) -> String {
        // 获取当前时间戳
        let currentTimestamp = TalkfunSharedTools.getCurrentDateString()
        
        // 计算时间差
        let interval = timestamp - currentTimestamp
        
        // 确保时间差为正值
        guard interval > 0 else {
            return String(format: "0小时:0分钟:0秒")
        }
        
        // 计算小时、分钟和秒
        let hours = Int(interval) / 3600
        let minutes = (Int(interval) % 3600) / 60
        let seconds = Int(interval) % 60
        
        // 格式化为 xx小时:xx分钟:xx秒 的形式
        return String(format: "%02d小时:%02d分钟:%02d秒", hours, minutes, seconds)
    }
    @objc public func setDateContent(startTime:NSInteger)
    {
        if (self.startTime != startTime ) {
            self.startTime = startTime
        }
    //00小时:15分钟:41秒
        let timeStr = self.timeIntervalString(until: startTime)
        self.dateContentLabel.text = timeStr

        if ( TalkfunSharedTools.getCurrentDateString() < startTime  && self.timer == nil) {
            self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
            RunLoop.main.add(self.timer! , forMode: RunLoop.Mode.default)
            
            //时钟
            self.clocKButton.isHidden = false
            //距离直播还有
            self.clocKLabel.isHidden = false
            //日期内容
            self.dateContentLabel.isHidden = false
         
        }
        else if (TalkfunSharedTools.getCurrentDateString() >= startTime ){
       
            self.timerStop()
            //主播不在
            self.leaveTemporarilyLabel.isHidden = false
        }

       
        
    }
    @objc public func timerAction()
    {
        self.setDateContent(startTime: self.startTime)
    }
  
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        var livingCloseY:CGFloat = 0
        let livingCloseH:CGFloat = 30
        if (self.isNotchScreen) {
            livingCloseY = self.mNavigationbarHeight
        }else{
           
            livingCloseY = 15
        }
        self.livingClose.frame = CGRect(x: self.frame.size.width - 30 - 15, y: livingCloseY, width: livingCloseH, height: livingCloseH)
       
        
        self.adListView.frame = CGRect(x: 0, y: livingCloseY, width: self.frame.size.width, height: self.frame.size.height - livingCloseY)
       
         
         self.livingRefresh.frame = CGRect(x: self.frame.size.width - 30 - 15, y: livingCloseY+30+15, width: livingCloseH, height: livingCloseH)
     
        if (self.zhuboView.frame.size.width > 0 ) {
            self.zhuboView.layer.cornerRadius = 20;
            
            self.avatarImage.layer.masksToBounds = true
            self.avatarImage.frame = CGRect(x: 3.5, y: 2.5, width: 35, height: 35)
    
            self.avatarImage.layer.cornerRadius = self.avatarImage.frame.size.height / 2.0;
            self.nicknameLabel.frame = CGRect(x: 45, y: 2.5, width: 81, height: 20)
        
            self.viewersLabel.frame = CGRect(x: 45, y: self.zhuboView.frame.size.height - 14 - 3, width: 47.5, height: 14)
            
            
            self.attentionButtonbackgroundColor.frame = CGRect(x: self.zhuboView.frame.size.width - 40 - 8, y: 6, width: 40, height: 28)
            self.attentionButton.frame = self.attentionButtonbackgroundColor.frame
            self.gl.frame =  self.attentionButton.bounds;
            self.gl.startPoint = CGPoint(x: 0.5, y: 0)
          
            self.gl.endPoint = CGPoint(x: 0.5, y: 1)
            self.gl.colors = [UIColor(red: 255/255.0, green: 172/255.0, blue: 77/255.0, alpha: 1.0).cgColor, UIColor(red: 255/255.0, green: 114/255.0, blue: 78/255.0, alpha: 0.3).cgColor]
            self.gl.locations = [0,1]
            self.gl.cornerRadius = 14
        }
        let clocKButtonY:CGFloat = 187.0/667.0*self.frame.size.height
        self.clocKButton.frame = CGRect(x: (self.frame.size.width - 35)/2, y: clocKButtonY, width: 35, height: 35)
      
        self.clocKLabel.frame = CGRect(x: 0, y: self.clocKButton.frame.maxY+15, width: self.frame.size.width, height: 22.5)
        
        self.dateContentLabel.frame = CGRect(x: 0, y: self.clocKLabel.frame.maxY+12, width: self.frame.size.width, height: 25)
        //加5 是为了不与弹窗关注,有重叠
        let spacing:CGFloat = -50;


        let leaveTemporarilyLabelY:CGFloat = 239.5/667.0*self.frame.size.height+spacing;
        self.leaveTemporarilyLabel.frame = CGRect(x: 0, y: leaveTemporarilyLabelY, width: self.frame.size.width, height: 25) //CGRectMake(0,leaveTemporarilyLabelY,self.frame.size.width,25);
        
        let liveBroadcastClosedY:CGFloat = 176/667.0*self.frame.size.height
        
        self.liveBroadcastClosedImage.frame = CGRect(x: (self.frame.size.width - 110)/2, y: liveBroadcastClosedY, width: 110, height: 110)
   
        self.liveBroadcastClosedLabel.frame = CGRect(x: 0, y: self.liveBroadcastClosedImage.frame.maxY+10, width: self.frame.size.width, height: 25)

        //榜单点击button
        self.invitationList.frame = CGRect(x: 0, y: self.zhuboView.frame.maxY+10, width: 95, height: 23)
        
        //滚动通知
        self.announceRollView.frame = CGRect(x: 0, y: self.invitationList.frame.maxY+10, width: self.frame.size.width, height: 36)
    
    }
    //直播结束是否隐藏
    @objc public func liveBroadcastClosed(hidde:Bool)
    {
        self.liveBroadcastClosedImage.isHidden = hidde
        self.liveBroadcastClosedLabel.isHidden = hidde
    }
    //时钟 距离直播还有 日期内容
    @objc public func clocKHidde(hidde:Bool)
    {
        self.clocKButton.isHidden = hidde
        self.clocKLabel.isHidden = hidde
        self.dateContentLabel.isHidden = hidde
        
    }
    //直播开始
    @objc public func liveStarted()
    {
        //时钟
        self.clocKButton.isHidden = true
        //距离直播还有
        self.clocKLabel.isHidden = true
        //日期内容
        self.dateContentLabel.isHidden = true
        //主播不在
        self.leaveTemporarilyLabel.isHidden = true
        //直播结束
        self.liveBroadcastClosedImage.isHidden = true
        //直播结束label
        self.liveBroadcastClosedLabel.isHidden = true
     
        
    }
}
